<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('新增库存转移')"/>

<body style="background-color: #f2f2f2;">

<div class="layui-fluid larry-wrapper">
    <input type="hidden" th:value="${id}" id="kid">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
            <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="form5">
                    <div class="layui-inline">
                        <label class="layui-form-label">提起人</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="tqrxm" name="tqrxm" lay-verify="required"
                                   placeholder="提起人"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">移动原因</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="ydyy" name="ydyy" lay-verify="required"
                                   placeholder="移动原因"/>
                        </div>
                    </div>
                </form>
                <div class="moreSearch" style="display: none">
                    <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                        <div class="layui-inline">
                            <label class="layui-form-label">货品编号</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="hpbh" name="hpbh" placeholder="货品编号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">货品名称</label>
                            <input type="text" class="layui-input" id="hpmc" name="hpmc" placeholder="货品名称"/>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">货品批次</label>
                            <input type="text" class="layui-input" id="hpph" name="hpph" placeholder="货品批次"/>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">生产日期</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" name="stime" id="stime"
                                       placeholder="yyyy-MM-dd HH:mm:ss"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"> </label>
                            <div class="layui-input-inline" style="height:20px">
                                --
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">&nbsp;</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="etime" name="etime"
                                       placeholder="yyyy-MM-dd HH:mm:ss"/>
                            </div>
                        </div>
                        <div class="layui-inline inline-search-btn">
                            <a class="layui-btn layui-btn-normal" data-type="reload">查询</a>
                        </div>
                        <div class="layui-inline inline-reset-btn">
                            <button type="reset" class="layui-btn layui-btn-primary" style="background-color: #40AFFE">
                                重置
                            </button>
                        </div>

                </div>
                </form>
                <div class="moreBtn"><i class="layui-icon layui-icon-triangle-d" style="color: #1E9FFF;"></i></div>
                <div class="group-button" id="toolbar">
                    <a class="layui-btn layui-btn-sm layui-btn-normal bc-btn">
                        <i class="layui-icon">&#x1005;</i> 保存
                    </a>
                </div>
                <!--列表-->
                <section class="panel panel-padding layui-card-body">
                    <div class="pro">
                        <table id="tab" class="table text-nowrap"></table>
                    </div>
                </section>
            </div>
        </div>
    </div>
    <!--select2查询库位-->
    <div id="kwList" style="display: none">
        <div class="layui-form-item">
            <form class="layui-form layui-card-header  layui-card-header-auto" id="selectForm">
                <!--<div>&nbsp;</div>-->
                <div class="layui-inline">
                    <label class="layui-form-label">目标储位</label>
                    <div>
                        <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="selectId"
                                name="ydhkwid" select2 style="width: 193px"></select>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<div th:include="/public/public_js::public_js"></div>
<script>
    $(function () {
        $(".moreBtn").on("click", function () {
            $(".moreSearch").toggle();
        });
    });
    var tab;
    var xx_number = 0;
    /*声明一个全局变量，来显示可移动数量*/
    layui.use(['list', 'layer', 'zzpUtils', 'jquery', 'laydate'], function () {
        window.operateEvent = {

            "click #btn_xz": function (e, value, row, index) {//选择
                btn_xz(row.id, row.hpbh, index);
            }
        }
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;

        //选择目标库位
        function btn_xz(id, hpbh, index) {
            var hpbh2 = hpbh;
            var index2 = index;
            layer.open({
                type: 1,
                title: '选择目标储位',
                area: ['30%', '30%'],
                //shade: 0.4,
                content: $("#kwList"),//支持获取DOM元素
                btn: ['保存', '取消'], //按钮
                yes: function (index) {
                    var zname = $('#selectId').text();
                    var str2value = $("#selectId").select2("val");
                    if (zname == '') {
                        layer.msg('目标储位不能为空！！！');
                    } else {
                        var pass = {};
                        pass.ydhkwid = str2value;
                        pass.show_ydhkwid = zname;
                        tab.bootstrapTable('updateRow', {index: index2, row: pass});
                        $("#selectId").val('');
                        $("#selectId").text('');
                        layer.close(index);
                    }
                },
                btn2: function (index) {
                    layer.close(index);
                    $("#selectId").text('');
                    $("#selectId").val('');
                }
            });
            //查询目标储位
            $(function () {
                $('.js-data-example-ajax').select2({
                    ajax: {
                        url: '/zxsdwms/wmsstockmove/searchKw',
                        dataType: 'json',
                        type: 'POST',
                        contentType: "application/json",
                        data: function (params) {
                            var query = {
                                show_ydhkwid: params.term,
                                hpbh: hpbh2
                            };
                            return JSON.stringify(query);
                        },
                        processResults: function (data) {
                            var arr = new Array();
                            if (data) {
                                for (var i = 0; i < data.length; i++) {
                                    arr.push({id: data[i].ydhkwid, text: data[i].show_ydhkwid});
                                }
                            }
                            return {
                                results: arr
                            };
                        },
                        cache: true
                    },
                    language: "zh-CN",
                    placeholder: '请输入查询字段',
                    allowClear: true
                });
            });
        }

        laydate.render({
            elem: '#stime'
            , theme: '#20a0ff'
        })
        laydate.render({
            elem: '#etime'
            , theme: '#20a0ff'
        })
        tab = zzpUtils.table({
            elem: '#tab',
            isApi: true,
            uniqueId: "id",
            pagination: true,
            url: '/wmsstockmove/searchKc',
            columns: [{
                field: '',
                checkbox: true
            }, {
                field: 'ydhkwid',
                title: '移动后库位id',
                visible: false
            }, {
                field: 'hpbh',
                title: '货品编号'
            }, {
                field: 'hpmc',
                title: '货品名称'
            }, {
                field: 'dwms',
                title: '单位描述'
            }, {
                field: 'hpph',
                title: '货品批次'
            }, {
                field: 'hpscrq',
                title: '生产日期'
            }, {
                field: 'show_kwid',
                title: '移动前库位'
            }, {
                field: 'kydsl',
                title: '可移动数量'
            }, {
                field: 'zysl',
                title: '移动数量',
                editable: {
                    type: 'text',
                    clear: false,
                    validate: function (value) {

                        if (isNaN(value)) return {newValue: 0, msg: '只允许输入数字'};
                        else if (value <= xx_number) return {newValue: value};
                        else if (value > xx_number) return {newValue: xx_number};
                    },
                }
            }, {
                field: 'show_ydhkwid',
                title: '目标储位'
            }, {
                field: 'cz',
                title: '操作',
                events: operateEvent,
                formatter: operateFormatter

            }],
            onClickRow: function (row, $element, field) {
                xx_number = row.kydsl;
            },
            queryParams: function () {//查询条件
                return zzpUtils.getFormJson($("#searchForm"));
            }

        });

        function operateFormatter(value, row, index) {
            return [
                '<a class="layui-btn layui-btn-xs" id="btn_xz">选择</a>'
            ].join('')
        }

        //查询
        $(".inline-search-btn").on('click', function () {
            zzpUtils.refreshTable("#tab");
        })
        /*保存*/
        $(".bc-btn").on('click', function () {
                var rows = $("#tab").bootstrapTable('getSelections');
                console.log(rows);
                var b = 0;
                var tqrxm = $("#tqrxm").val();
                var ydyy = $("#ydyy").val();
                if (tqrxm != '' && ydyy != '') {
                    if (rows.length == 0) {
                        layer.msg("请选择一条进行转移");
                    } else {
                        for (var i = 0; i < rows.length; i++) {
                            if (rows[i].show_ydhkwid == '' || rows[i].zysl == 0) {
                                b++;
                            }
                        }
                        if (b <= 0) {
                            zzpUtils.confirm("您确认要保存吗", function (index) {
                                zzpUtils.action({
                                    url: "/wmsstockmove/add",
                                    data: JSON.stringify({list: rows, tqrxm: tqrxm, ydyy: ydyy}),
                                    success: function (data) {
                                        setTimeout(function () {
                                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                            parent.layer.close(index); //再执行关闭
                                            top.layer.msg(data.title);
                                        }, 500);
                                    }, error: function (xhr, textStatus) {
                                        console.log(textStatus);
                                        console.log(xhr)
                                    }
                                });
                            });
                        } else {
                            layer.msg("移动数量必须大于0或者目标储位不能为空！");
                        }
                    }
                }
                else {
                    layer.msg("提起人和移动原因不能为空");
                }
            }
        )
    });

</script>
</html>